# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
#处理数据 比如：写入文件、写入数据库、先清洗
import csv
# import MySQLdb
class LylPipeline:
    def __init__(self):
        # 连接数据库
        self.conn=pymysql.connect(host = 'localhost',user = 'root',password = 'hwj99999',database = 'profile',charset = 'utf8',port=3306)
        # 2. 创建一个游标cursor, 用来操作表。
        self.cursor = self.conn.cursor()
        #将爬取的数据保存到文件中
        self.f = open("./computer.csv", "w", newline='',encoding="utf-8")#newline去除空行
        self.writer = csv.writer(self.f)
        self.writer.writerow(('bookname', 'link', 'details', 'uploader', 'view'))#标头
        # try:
        # self.cursor.execute('create table sources(bookname varchar(20),link varchar(100),details varchar(1000),uploader varchar(20) default "wj",\
        #      view int(20) default 0)')
        # except:
        #    except self.cursor.execute('delete from sources')

    def process_item(self, item, spider):
        # 3. 将Item数据放入数据库，默认是同步写入。
        insert_sql = "INSERT INTO  sources VALUES('%s','%s','%s','%s','%s')" % (item['bookname'], item['link'],
                            item['details'], item['uploader'], item['view'])
        self.cursor.execute(insert_sql)
        self.writer.writerow((item['bookname'], item['link'],
                            item['details'], item['uploader'], item['view']))
        # return item
        # 4. 提交操作
        self.conn.commit()

    def close_spider(self, spider):
        self.cursor.close()
        self.conn.close()
        self.f.close()
    # def process_item(self, item, spider):
    #     return item




